{% from "../../macros/attributes.njk" import govukAttributes %}
{% from "../tag/macro.njk" import govukTag %}

{%- set idPrefix = params.idPrefix if params.idPrefix else "task-list" -%}

{%- macro _taskListItem(params, item, index) %}
  {%- set hintId = idPrefix + "-" + index + "-hint" %}
  {%- set statusId = idPrefix + "-" + index + "-status" %}
  <li class="govuk-task-list__item {%- if item.href %} govuk-task-list__item--with-link{% endif %}{%- if item.classes %} {{ item.classes }}{% endif %}">
    <div class="govuk-task-list__name-and-hint">
    {% if item.href %}
      <a class="govuk-link govuk-task-list__link {%- if item.title.classes %} {{ item.title.classes }}{% endif %}" href="{{ item.href }}" aria-describedby="{{ hintId + " " if item.hint }}{{ statusId }}">
        {{ item.title.html | safe | trim | indent(8) if item.title.html else item.title.text }}
      </a>
    {% else %}
      <div {%- if item.title.classes %} class="{{ item.title.classes }}"{% endif %}>
        {{ item.title.html | safe | trim | indent(8) if item.title.html else item.title.text }}
      </div>
    {% endif %}
    {% if item.hint %}
      <div id="{{ hintId }}" class="govuk-task-list__hint">
        {{ item.hint.html | safe | trim | indent(8) if item.hint.html else item.hint.text }}
      </div>
    {% endif %}
    </div>
    <div class="govuk-task-list__status {%- if item.status.classes %} {{ item.status.classes }}{% endif %}" id="{{ statusId }}">
    {% if item.status.tag %}
      {{ govukTag(item.status.tag) | trim | indent(6) }}
    {% else %}
      {{ item.status.html | safe | trim | indent(6) if item.status.html else item.status.text }}
    {% endif %}
    </div>
  </li>
{%- endmacro %}

<ul class="govuk-task-list {%- if params.classes %} {{ params.classes }}{% endif %}"
  {{- govukAttributes(params.attributes) }}>
  {% for item in params.items %}
    {{- _taskListItem(params, item, loop.index) if item }}
  {% endfor %}
</ul>
